//
//  TVProductCellModel.swift
//  StarTimesON
//
//  Created by Toj on 12/7/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class TVProductCellModel: NSObject {

    var isLoaded = false
    
    private(set) var commodityDictionary: [String: TVCommodityCellModel] = [:]
    private(set) var commodities: [TVCommodityCellModel] = []
    
    var selectedCommodity: TVCommodityCellModel?
    var selectedIndex: Int {
        var selIndex = -1
        for commodity in commodities {
            selIndex += 1
            guard selectedCommodity == commodity else { continue }
            break
        }
        
        return selIndex
    }
    
    init(cardInfo: TVSmartcardInfo) {
        super.init()
        
        var commodityList = [TVCommodityCellModel]()
        var commodityDictionary_ = [String: TVCommodityCellModel]()
        cardInfo.recharge_items?.forEach( { item in
            let commodity = TVCommodityCellModel(commodity: item)
            let id = commodity.id
            commodityList.append(commodity)
            commodityDictionary_[id] = commodity
            guard commodity.isSelected else { return }
            // 默认选项在外侧统一处理, 这里只是默认值
            commodity.isSelected = false
            selectedCommodity = commodity
        })
        commodities = commodityList
        commodityDictionary = commodityDictionary_
        
        guard selectedCommodity == nil else { return }
        selectedCommodity = commodities.first
    }
}
